package com.jzt.qf.Thread;

import com.alibaba.fastjson.JSON;
import com.jzt.qf.beans.HttpResult;
import com.jzt.qf.beans.UploadBase;
import com.jzt.qf.beans.jztBean.GET_InStock;
import com.jzt.qf.beans.jztBean.JztRKJson;
import com.jzt.qf.beans.qfBean.ZXY_GJ_RK_SC_BILL_Bean;
import com.jzt.qf.beans.qfBean.ZxyUploadbillJzt;
import com.jzt.qf.beans.uploadJson.JztJsRecordsJson;
import com.jzt.qf.dao.BuyBillDao;
import com.jzt.qf.dao.UploadbillJztDao;
import com.jzt.qf.service.GetService.GetInStockService;
import com.jzt.qf.service.GetService.GetJLService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 　* @description:采购数据处理线程
 * 　* @author zxy
 * 　* @date 2020-08-05 16:44
 */
@Component
@Slf4j
public class CGBillThread extends Thread {
    private UploadbillJztDao zxyUploadbillDao;
    private BuyBillDao buyBillDao;
    private GetInStockService getInStockService;
    private GetJLService getJLService;

    public CGBillThread(UploadbillJztDao zxyUploadbillDao, BuyBillDao buyBillDao, GetInStockService getInStockService, GetJLService getJLService)

    {
        this.zxyUploadbillDao = zxyUploadbillDao;
        this.buyBillDao = buyBillDao;
        this.getInStockService = getInStockService;
        this.getJLService = getJLService;
    }

    @SneakyThrows
    @Override
    public void run() {
        log.info("处理采购订单回传数据");
        List<ZxyUploadbillJzt> jzt_buyBills = zxyUploadbillDao.getBackWriteBillByBilltype(34);
        for (ZxyUploadbillJzt jzt_buyBill : jzt_buyBills) {
            //TODO 获取拒收记录
            JztJsRecordsJson jsRecordsJson = new JztJsRecordsJson(jzt_buyBill.getBillcode());
            UploadBase<JztJsRecordsJson> uploadBase = new UploadBase("ypjsjldhcx", "select", jsRecordsJson);
            getJLService.getJSRecords(uploadBase);
            //TODO 获取拒收记录
            //TODO 线程要休息,不休息这里如果是数据量大,一直重复抓取数据，
            JztRKJson jztRKJson = new JztRKJson(jzt_buyBill.getBillcode(), "1");
            UploadBase<JztRKJson> jztRKJsonUploadBase = new UploadBase("rksc", "select", jztRKJson);
            HttpResult<List<GET_InStock>> httpResult = getInStockService.getBuy(jztRKJsonUploadBase);
            if (httpResult.isFlag()) {
                List<GET_InStock> inStocks = httpResult.getMsgInfo();
                for (int i = 0; i < inStocks.size(); i++) {
//                  log.info(JSON.toJSONString(inStocks.get(i)));\
                    GET_InStock get_inStock = JSON.parseObject(JSON.toJSONString(inStocks.get(i)), GET_InStock.class);
                    ZXY_GJ_RK_SC_BILL_Bean rk_scbill = new ZXY_GJ_RK_SC_BILL_Bean();
                    rk_scbill.setDANJ_NO(get_inStock.getInstoreBill_No_LMIS());//九州通单据编号
                    rk_scbill.setPicihao(get_inStock.getGoods_Lotno());//批次号
                    rk_scbill.setYx_date(get_inStock.getValid_Until());//有效期
                    rk_scbill.setSc_date(get_inStock.getProduction_Date());//生产日期
                    rk_scbill.setSs_qty(get_inStock.getStorage_Quantity() + "");//入库数量（实收数量）
                    rk_scbill.setCHOUY_QTY(get_inStock.getSpotCheck_Qty());//抽样数量
                    rk_scbill.setPRICE(get_inStock.getPrice() + "");//单价
                    rk_scbill.setBillid(get_inStock.getBill_Dtl_Id_Old().equals("39150")?"39461":get_inStock.getBill_Dtl_Id_Old());//单据BillId
                    rk_scbill.setPrec(get_inStock.getGoods_No());//商品编号
                    rk_scbill.setChecker(get_inStock.getQuality_Inspector());//质检员（验收人）
                    rk_scbill.setSHR(get_inStock.getReceiving_Clerk());//收货员
                    rk_scbill.setRIQI_CHAR(get_inStock.getAcceptance_Check_Date());//验收日期
                    rk_scbill.setRUK_TYPE(1);
                    rk_scbill.setOrd(Integer.parseInt(get_inStock.getBill_Dtl_Id_Old()));//行号
                    rk_scbill.setYANS_RLT(Integer.parseInt(get_inStock.getAcceptance_Check_Rlt()));
                    rk_scbill.setJzt_id(get_inStock.getBill_Dtl_Id_jzyc());
                    rk_scbill.setJztResult("");
                    //TODO 2023-08-24新加入库的运输相关数据
                    rk_scbill.setYS_Weway(get_inStock.getTransport_Mode());//运输方式
                    rk_scbill.setYS_DW(get_inStock.getTransport_Ssa());//运输单位
                    rk_scbill.setYS_Date(get_inStock.getTransport_Time());//启运时间
                    rk_scbill.setYS_WKFS(get_inStock.getTprt_Mode());//温控方式
                    rk_scbill.setYS_DHWD(get_inStock.getTprt_Record());//到货温度
                    rk_scbill.setYS_ADDR(get_inStock.getDeparture_Address());//运输地址(启运地点)
                    if (rk_scbill.getYANS_RLT() == 1 || rk_scbill.getYANS_RLT() == 2 || rk_scbill.getYANS_RLT() == 3) {
                        int flags = buyBillDao.ZXY_GJ_RK_SC_BILL(rk_scbill);
                        log.info("入库千方存储过程返回的结果集" + flags);
                        //TODO 如果返回的flags> 0 证明数据处理成功,需要关单
//                        if (flags > 0) {
//                            getInStockService.closeBuyBillByBill_Dtl_Id_jzyc(get_inStock.getBill_Dtl_Id_jzyc());
//                        }
                    }
                }
            }
        }
        log.info("处理采购订单回传数据结束");
    }
}
